//
//  MsgHistoryWindowController.m
//  HiQ
//
//  Created by scarlettsun on 12-8-9.
//  Copyright (c) 2012年 tencent. All rights reserved.
//

#import "MsgHistoryWindowController.h"
#import "Buddy.h"
#import "BuddyGroup.h"
#import "ImageAndTextCell.h"
#import "MainWindowController.h"
#import "FileAssistant.h"

@implementation MsgHistoryWindowController
@synthesize contactView;
@synthesize msgView;

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];
    if (self) 
    {
        contactSource = [[ContactListSource alloc] init];        
        [contactSource.buddyGroupArray addObject:[[[[MainWindowController sharedMainWindowController] buddySource] buddyGroupArray] objectAtIndex:0]];
        [contactSource.buddyGroupArray addObject:[[[[MainWindowController sharedMainWindowController] clusterSource] clusterGroupArray] objectAtIndex:0]];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:self.window];
    }
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)windowDidLoad
{
    [super windowDidLoad];
}

- (void)windowWillClose:(NSNotification *)notification 
{
    if ([notification object] == self.window) 
    {
        ((MainWindowController*)[MainWindowController sharedMainWindowController]).msgWindowController = nil; 
    }
}

//初始化显示消息记录
- (void)initContact:(id) contact
{
    NSUInteger index = 0;
    id firstItem = [contactView itemAtRow:0];
    if ([contact isKindOfClass:[Buddy class]]) 
    {
       //显示好友消息记录
        NSMutableArray * contactlist =((BuddyGroup*)[contactSource.buddyGroupArray objectAtIndex:0]).buddyList ;
        for (int i = 0; i< [contactlist count]; i++) 
        {       
            if (((Buddy*)[contactlist objectAtIndex:i]).buddyQQ == ((Buddy*)contact).buddyQQ) 
            {
                [contactView expandItem:firstItem];
                index += (i+1);
                break;
            }
        }
        [self showBuddyMsgHistory:(Buddy*)contact];
    }
    if ([contact isKindOfClass:[Cluster class]]) 
    {
        //显示群消息记录 
        id secondItem = nil;
        if ([contactView isItemExpanded:firstItem]) {
            index += (((BuddyGroup*)[contactSource.buddyGroupArray objectAtIndex:0]).buddyList.count +1);
        }
        else 
        {
            index += 1;
        }
        secondItem = [contactView itemAtRow:index];
        [contactView expandItem:secondItem];
        NSMutableArray * clusterlist = ((ClusterGroup*)[contactSource.buddyGroupArray objectAtIndex:1])->clusterList ;
        for (int i = 0; i< [clusterlist count]; i++) 
        {       
            if (((Cluster*)[clusterlist objectAtIndex:i]).clusterId == ((Cluster*)contact).clusterId) 
            {
                index += (i+1);
                break;
            }
        }
        [self showGroupMsgHistory:(Cluster*)contact];
    }  
    [contactView deselectAll:self];
    NSIndexSet *indexset = [NSIndexSet indexSetWithIndex:index];
    [contactView selectRowIndexes:indexset byExtendingSelection:YES];
    
}

-(void) awakeFromNib
{
    NSTableColumn* column = [contactView tableColumnWithIdentifier:@"BuddyList"];
    ImageAndTextCell* cell = [[ImageAndTextCell alloc] init];
    [column setDataCell: cell]; 
    [contactView setDataSource:(id<NSOutlineViewDataSource>)contactSource];  // 设置 buddyList 的 data source
    [contactView setDelegate:(id<NSOutlineViewDelegate>)self]; // 设置 buddyList 的 delegate
    [contactView setAction:@selector(onClickBuddyOutlineItem:)];
    [msgView setEditable:NO];
    [contactView reloadData];
    [contactView display];
}

- (void)onClickBuddyOutlineItem:(id)sender
{
    // 展开或收起好友列表
    // 双击好友，打开聊天窗口
    id selectedItem = [contactView itemAtRow:[sender clickedRow]];
    if(selectedItem == nil)
        return;
    if([contactView isExpandable:selectedItem])
        return;
    if ([selectedItem isKindOfClass:[Buddy class]]) {
        [self showBuddyMsgHistory:(Buddy *)selectedItem];
    }
    if ([selectedItem isKindOfClass:[Cluster class]]) {
        [self showGroupMsgHistory:(Cluster *)selectedItem];
    }
}

- (void) showBuddyMsgHistory:(Buddy *)peerInfo
{
    Buddy *myinfo = (Buddy*)[MainWindowController shareMyselfInfo];
    NSString *msg = [FileAssistant readData:[NSString stringWithFormat:@"%li",peerInfo.buddyQQ] selfQQ:[NSString stringWithFormat:@"%li",myinfo.buddyQQ]];
    if (msg == nil)
    {
        msg = [NSString stringWithFormat:NSLocalizedString(@"暂无记录", nil)];
    }
    [msgView setString:msg];
}

- (void) showGroupMsgHistory:(Cluster *)groupInfo
{
    Buddy *myinfo = (Buddy*)[MainWindowController shareMyselfInfo];
    NSString *msg = [FileAssistant readData:[NSString stringWithFormat:@"%li",groupInfo.clusterId] selfQQ:[NSString stringWithFormat:@"%li",myinfo.buddyQQ]];
    if (msg == nil)
    {
        msg = [NSString stringWithFormat:NSLocalizedString(@"暂无记录", nil)];
    }
    [msgView setString:msg];
}


- (BOOL)outlineView:(NSOutlineView *)outlineView shouldEditTableColumn:(NSTableColumn *)tableColumn item:(id)item
{
    return NO;
}

- (void)outlineView:(NSOutlineView*)olv willDisplayCell:(NSCell*)cell forTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
   if ([[tableColumn identifier] isEqualToString: @"BuddyList"])
	{
        if([item isKindOfClass:[Buddy class]])
        {
            NSMutableString *buddyName = [[NSMutableString alloc] initWithString:((Buddy*)item)->buddyName];
            //if (((Buddy*)item)->status == BUDDY_STATUS_ONLINE)
                [(ImageAndTextCell*)cell setImage: ((Buddy*)item)->onPhoto];  
           // else
               // [(ImageAndTextCell*)cell setImage: ((Buddy*)item)->offPhoto];
            [(ImageAndTextCell*)cell setStringValue:buddyName];
        }
        
        if([item isKindOfClass:[Cluster class]])
        {
            [(ImageAndTextCell*)cell setImage:[NSImage imageNamed:((Cluster*)item)->photo]];
            [(ImageAndTextCell*)cell setStringValue:((Cluster*)item)->clusterName];
        }
        
        if(([item isKindOfClass:[BuddyGroup class]])|| ([item isKindOfClass:[ClusterGroup class]]))
        {
            [(ImageAndTextCell*)cell setImage: nil];
        }
        
    }
}
@end
